﻿#include "mymainwindow.h"
#include "ui_mymainwindow.h"
#include <QPalette>
#include <QBrush>
#include <QQmlEngine>
#include <QQmlContext>
#include "mylisten.h"
#include <QDir>

MyMainWindow::MyMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setWindowFlags(windowFlags() | Qt::SubWindow | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);

    setAttribute(Qt::WA_TranslucentBackground);

    setAutoFillBackground(true);

    QPalette palette;
    palette.setBrush(QPalette::Window, QBrush(Qt::transparent));
    setPalette(palette);

    ui->quickWidget->engine()->rootContext()->setContextProperty("widget", this);
    ui->quickWidget->engine()->rootContext()->setContextProperty("currentPath", QDir::currentPath());
//    ui->quickWidget->engine()->rootContext()->setContextProperty("MyListen", &MyListen::instance());

    mousePointChanged();

    ui->quickWidget->setClearColor(QColor(Qt::transparent));    //qml 透明
    ui->quickWidget->setSource(QUrl("qrc:/main.qml"));

    connect(&MyListen::instance(), &MyListen::mousePointChanged, this, &MyMainWindow::mousePointChanged);

    setSystemMenu();

    setFixedSize(ui->quickWidget->size());

}

MyMainWindow::~MyMainWindow()
{
    delete trayIcon;
    delete ui;
}

//添加系统托盘
void MyMainWindow::setSystemMenu()
{
    trayIcon = new QSystemTrayIcon();

    trayIcon->setIcon(QIcon(":/logo.ico"));

    QMenu *menu = new QMenu();

    action_exit = menu->addAction("quit");

    connect(action_exit,SIGNAL(triggered(bool)),this,SLOT(systemExit(bool)));

    trayIcon->setContextMenu(menu);

    trayIcon->show();
}

void MyMainWindow::systemExit(bool)
{
    qApp->quit();
}

void MyMainWindow::mousePointChanged()
{
    QPoint p = MyListen::instance().mousePoint();
    move(p.x()+5, p.y()+5);
}
